//
//  xiangqingVC.m
//  lyxluyou
//
//  Created by mmyy on 15/10/15.
//  Copyright (c) 2015年 mmyy. All rights reserved.
//

#import "xiangqingVC.h"
#import "xiangqinItems.h"
#import "xiangqinCell.h"
#import "xinchengVC.h"
@interface xiangqingVC ()<UITableViewDataSource, UITableViewDelegate>

@end

@implementation xiangqingVC
{
    UITableView *_tableView;
    NSMutableArray *_dataSource;
    int count;
    BOOL isUpData;
    UIImageView *_bgImage;

}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    count = 5;
    isUpData = YES;
    self.navigationItem.title = [NSString stringWithFormat:@"游记(%@)", self.titleName];
    self.navigationController.navigationBar.translucent = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self createloadDataImage];
    [self createTableView];
    [self loaddata];
    
    
    // Do any additional setup after loading the view.
}
-(void)createloadDataImage
{
    _bgImage = [loadImage loadImage];
    _bgImage.frame = CGRectMake((self.view.frame.size.width - 100) / 2.0, (self.view.frame.size.height - 100) / 2, 100, 100);
    [self.view addSubview:_bgImage];
}
#pragma mark --loadData
-(void)loaddata
{
    _bgImage.hidden = NO;
    [self.view bringSubviewToFront:_bgImage];
    NSString *url = [NSString stringWithFormat:xiangqingUrl,self.vid, count];
    [Funcation getWithURL:url success:^(id responseObject) {
        _bgImage.hidden = YES;
        _dataSource = [NSMutableArray arrayWithCapacity:0];
        NSArray *trips = [responseObject objectForKey:@"trips"];
        for (NSDictionary *dict in trips) {
            xiangqinItems *items = [xiangqinItems xiangqinItemsWithDict:dict];
            [_dataSource addObject:items];
        }
        [_tableView reloadData];
        if (isUpData) {
            [_tableView headerEndRefreshing];
            [_tableView footerEndRefreshing];
            isUpData = NO;
        }
    } andFail:^(id error) {
        
    }];

}

#pragma mark -- createTableView
-(void)createTableView
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 65, self.view.frame.size.width, self.view.frame.size.height - 65) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [self createMjRefresh];

}
-(void)createMjRefresh
{
    __weak xiangqingVC *viewC = self;
    __weak UITableView * mytable = _tableView;
    [_tableView addHeaderWithCallback:^{
        mytable.headerRefreshingText = @"正在刷新中...";
        isUpData = YES;
        [viewC loaddata];
    }];
    [_tableView addFooterWithCallback:^{
        count += 10;
        isUpData = YES;
        [viewC loaddata];
        mytable.footerRefreshingText = @"正在加载更多";
    }];

}
#pragma mark -- tableViewDelegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    xiangqinItems *items = [_dataSource objectAtIndex:indexPath.row];
    if ([items.trip_text length] == 0) {
        return 80 + 5 + 5 + 40 + 10;
    }else {
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;

        NSDictionary *attrDict = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:10],
                                  NSFontAttributeName,
                                  paragraphStyle,
                                  NSParagraphStyleAttributeName,
                                  nil];
        CGRect expectedRect = [items.trip_text boundingRectWithSize:CGSizeMake(300, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrDict context:nil];
        CGSize expectedSize = expectedRect.size;
        return 80 + 5 + 5 + 40 + 10 + 5 + expectedSize.height;
    
    }

}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSource.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    xiangqinCell *cell = (xiangqinCell *)[tableView cellForRowAtIndexPath:indexPath];
    if (!cell) {
        cell = [[xiangqinCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"myCell"];
    }
    
    xiangqinItems *items = [_dataSource objectAtIndex:indexPath.row];
    [cell.headImage setImageWithURL:[NSURL URLWithString:items.avatar_m]];
    if (![items.name isKindOfClass:[NSNull class]]) {
        cell.vname.text = items.name;
    }
    if (![items.uname isKindOfClass:[NSNull class]]) {
        cell.uname.text = items.uname;
    }
    if (![items.datetime isKindOfClass:[NSNull class]]) {
        cell.detaTime.text = items.datetime;
    }
//    
    if ([items.trip_text length] != 0) {
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
        
        NSDictionary *attrDict = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:10],
                                  NSFontAttributeName,
                                  paragraphStyle,
                                  NSParagraphStyleAttributeName,
                                  nil];
        CGRect expectedRect = [items.trip_text boundingRectWithSize:CGSizeMake(300, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrDict context:nil];
        CGSize expectedSize = expectedRect.size;
    
        [cell.trip mas_makeConstraints:^(MASConstraintMaker *make) {
            [cell.trip mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(cell.headImage.mas_bottom).offset(5);
                make.left.equalTo(cell.mas_left).offset(10);
                make.right.equalTo(cell.mas_right).offset(-10);
                make.height.equalTo(expectedSize.height);
            }];
        }];
        cell.trip.text = items.trip_text;
    }
    if([items.trip_text length] == 0)
    {
        [cell.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(cell.headImage.mas_bottom).offset(5);
            make.left.equalTo(cell.mas_left).offset(10);
            make.height.equalTo(@80);
            make.right.equalTo(cell.mas_right).offset(0);
        }];
    
    
    }else{
        [cell.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(cell.trip.mas_bottom).offset(5);
            make.left.equalTo(cell.mas_left).offset(10);
            make.height.equalTo(@80);
            make.right.equalTo(cell.mas_right).offset(0);
        }];
    }
    for (int i = 0; i < items.waypoint.count; i++) {
        UIImageView *imageV = [UIImageView new];
        NSInteger imageW = 60;
        imageV.backgroundColor = [UIColor orangeColor];
        [imageV setImageWithURL:[NSURL URLWithString:[items.waypoint objectAtIndex:i]] placeholderImage:[UIImage imageNamed:@"define"]];
        [cell.scrollView addSubview:imageV];
        [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(cell.scrollView.top).offset(0);
            make.left.equalTo(cell.scrollView.left).offset(0 + i * (imageW + 5));
            make.width.equalTo(@60);
            make.height.equalTo(@80);
            if (i == items.waypoint.count - 1) {
                make.right.equalTo(cell.scrollView.right).offset(0);
            }
        }];
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    xinchengVC *xincheng = [[xinchengVC alloc] init];
    xincheng.vid = [[_dataSource objectAtIndex:indexPath.row] vid];
    [self.navigationController pushViewController:xincheng animated:YES];
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
